package com.zbkj.common.utils;

import com.anji.captcha.util.StringUtils;
import com.zbkj.common.vo.InspectionVo;

import java.io.InputStream;
import java.util.*;

public class ToInspectionPTFUtil {

    /**
     * 生成验光仪检测报告单 PDF
     *
     * @param vo 验光报告参数params
     * @return 生成的 PDF 文件路径
     */
    public static String generateInspectionPDF(InspectionVo vo, String savePath, String saveName) {
        // 默认参数填充
        Map<String, Object> parameters = new HashMap<>();

        // 设置默认值防止空指针
        parameters.put("name", StringUtils.isBlank(vo.getName()) ? "" : vo.getName());
        parameters.put("sampleNum", StringUtils.isBlank(vo.getSampleNum()) ? "" : vo.getSampleNum());
        parameters.put("opeUsername", StringUtils.isBlank(vo.getOpeUsername()) ? "" : vo.getOpeUsername());
        parameters.put("sex", StringUtils.isBlank(vo.getSex()) ? "" : vo.getSex());
        parameters.put("instrumentNum", StringUtils.isBlank(vo.getInstrumentNum()) ? "" : vo.getInstrumentNum());
        parameters.put("opeTime", StringUtils.isBlank(vo.getOpeTime()) ? "" : vo.getOpeTime());
        parameters.put("birthDate", StringUtils.isBlank(vo.getBirthDate()) ? "" : vo.getBirthDate());
        parameters.put("opeDate", StringUtils.isBlank(vo.getOpeDate()) ? "" : vo.getOpeDate());

        // 屈光度OD
        parameters.put("diopterODS1", vo.getDiopterODSList().get(0));
        parameters.put("diopterODS2", vo.getDiopterODSList().size() > 1 && Objects.nonNull(vo.getDiopterODSList().get(1)) ? vo.getDiopterODSList().get(1) : "");
        parameters.put("diopterODC1", vo.getDiopterODCList().get(0));
        parameters.put("diopterODC2", vo.getDiopterODCList().size() > 1 && Objects.nonNull(vo.getDiopterODCList().get(1)) ? vo.getDiopterODCList().get(1) : "");
        parameters.put("diopterODA1", vo.getDiopterODAList().get(0));
        parameters.put("diopterODA2", vo.getDiopterODAList().size() > 1 && Objects.nonNull(vo.getDiopterODAList().get(1)) ? vo.getDiopterODAList().get(1) : "");
        parameters.put("diopterODSE", vo.getDiopterODSE());

        // 屈光度OS
        parameters.put("diopterOSS1", vo.getDiopterOSSList().get(0));
        parameters.put("diopterOSS2", vo.getDiopterOSSList().size() > 1 && Objects.nonNull(vo.getDiopterOSSList().get(1)) ? vo.getDiopterOSSList() : "");
        parameters.put("diopterOSC1", vo.getDiopterOSCList().get(0));
        parameters.put("diopterOSC2", vo.getDiopterOSCList().size() > 1 && Objects.nonNull(vo.getDiopterOSCList().get(1)) ? vo.getDiopterOSCList() : "");
        parameters.put("diopterOSA1", vo.getDiopterOSAList().get(0));
        parameters.put("diopterOSA2", vo.getDiopterOSAList().size() > 1 && Objects.nonNull(vo.getDiopterOSAList().get(1)) ? vo.getDiopterOSAList().get(1) : "");
        parameters.put("diopterOSSE", vo.getDiopterOSSE());

        // 角膜值OD
        parameters.put("cornealODRK11", vo.getCornealODRK1List().get(0));
        parameters.put("cornealODRK12", vo.getCornealODRK1List().size() > 1 && Objects.nonNull(vo.getCornealODRK1List().get(1)) ? vo.getCornealODRK1List().get(1) : "");
        parameters.put("cornealODRK21", vo.getCornealODRK2List().get(0));
        parameters.put("cornealODRK22", vo.getCornealODRK2List().size() > 1 && Objects.nonNull(vo.getCornealODRK2List().get(1)) ? vo.getCornealODRK2List().get(1) : "");
        parameters.put("cornealODA1", vo.getCornealODAList().get(0));
        parameters.put("cornealODA2", vo.getCornealODAList().size() > 1 && Objects.nonNull(vo.getCornealODAList().get(1)) ? vo.getCornealODAList().get(1) : "");
        parameters.put("cornealODCYL", vo.getCornealODCYL());

        // 角膜值OS
        parameters.put("cornealOSRK11", vo.getCornealOSRK1List().get(0));
        parameters.put("cornealOSRK12", vo.getCornealOSRK1List().size() > 1 && Objects.nonNull(vo.getCornealOSRK1List().get(1)) ? vo.getCornealOSRK1List().get(1) : "");
        parameters.put("cornealOSRK21", vo.getCornealOSRK2List().get(0));
        parameters.put("cornealOSRK22", vo.getCornealOSRK2List().size() > 1 && Objects.nonNull(vo.getCornealOSRK2List().get(1)) ? vo.getCornealOSRK2List().get(1) : "");
        parameters.put("cornealOSA1", vo.getCornealOSAList().get(0));
        parameters.put("cornealOSA2", vo.getCornealOSAList().size() > 1 && Objects.nonNull(vo.getCornealOSAList().get(1)) ? vo.getCornealOSAList().get(1) : "");
        parameters.put("cornealOSCYL", vo.getCornealOSCYL());

        // 加载 企业logo图片
        if (StringUtils.isNotBlank(vo.getLogoImgPath())) {
            InputStream logoInputStream = ToInspectionPTFUtil.class.getClassLoader().getResourceAsStream(vo.getLogoImgPath());
            if (logoInputStream != null) {
                parameters.put("logo", logoInputStream);
            }
        }

        // VD、PD
        parameters.put("vd", vo.getVd());
        parameters.put("pd", vo.getPd());

        // 数据源（可为空）
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 调用工具类生成 PDF
        return JasperReportUtil.generatePdfFromJrxml(
                "jasperReport/InspectionReport.jrxml",
                savePath,
                parameters,
                dataList,
                saveName);
    }

    // 测试
    public static void main(String[] args) {
        InspectionVo inspectionVo = InspectionVo.buildSelfTest();
//        System.out.println(generateInspectionPDF(inspectionVo));

        String outputPath = generateInspectionPDF(inspectionVo, "D:/", "视力检测报告");
        System.out.println("PDF 已生成至：" + outputPath);

    }
}
